#!/usr/bin/env coffee

> @swc/core:swc
  path > join basename
  @3-/walk
  @3-/read
  @3-/write
  @3-/coffee_plus
  coffeescript:CoffeeScript

compile = CoffeePlus(CoffeeScript)

{dirname: ROOT} = import.meta
LIB = join(ROOT, "lib")
SRC = join(ROOT, "src")

for await fp from walk(SRC)
  if fp.endsWith ".coffee"
    out = compile read(fp)
    {
      map
      code
    } = await swc
      .transform(
        out
        {
          filename:basename(fp)
          sourceMaps:true
          jsc: {
            minify: {
              compress: {
                unused: true
                drop_console: true
                drop_debugger: true
              }
              mangle: true
            }
          }
          minify: true
          env:
            targets: "> 1%"
        }
      )
    js = join(LIB, fp.slice(SRC.length+1, -7) + ".js")
    write(js,code)
    write(js+'.map',map)

pkg_json = 'package.json'
fp = join ROOT,'lib.'+pkg_json
package_json = JSON.parse read(fp)
li = package_json.version.split('.').map((i)=>i-0)
li[2] = li[2]+1
package_json.version = li.join '.'
package_json = JSON.stringify package_json,null,2

for i from [
  fp
  join(LIB,pkg_json)
]
  write(i,package_json)

